package com.xk857.simpleeffect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xk857.simpleeffect.R;
import com.xk857.simpleeffect.domain.Result;
import com.xk857.simpleeffect.domain.param.DeleteBusinessCustomerParam;
import com.xk857.simpleeffect.domain.param.DeleteProjectCustomerParam;
import com.xk857.simpleeffect.domain.vo.CustomerBusinessSimpleVO;
import com.xk857.simpleeffect.enums.RequestEnum;
import com.xk857.simpleeffect.utils.OkHttpUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.lang.reflect.Type;
import java.util.function.Consumer;

import de.hdodenhof.circleimageview.CircleImageView;

public class ProjectPersonInfoActivity extends AppCompatActivity {

    private TitleBar titleBar;
    private LinearLayout linearLayout5;
    private CircleImageView headImgView;
    private TextView nickNameText;
    private LinearLayout updateNickNameLayout;
    private LinearLayout deleteLayout;
    // 待办true 项目false
    private boolean isBusiness;
    private CustomerBusinessSimpleVO simpleCustomer;
    private Long businessId;
    private Integer projectId;
    private RoundButton adminBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.white));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_project_person_info);
        initView();
        initData();
    }

    private void initData() {
        // 如果是待办传true，如果是项目传false
        isBusiness = getIntent().getBooleanExtra("isBusiness", true);
        // 待办id或项目id
        businessId = getIntent().getLongExtra("businessId", -1);
        projectId = getIntent().getIntExtra("projectId", -1);
        Gson gson = new Gson();
        simpleCustomer = gson.fromJson(getIntent().getStringExtra("info"), CustomerBusinessSimpleVO.class);
        Glide.with(this).load(simpleCustomer.getHeadImg()).into(headImgView);
        nickNameText.setText(simpleCustomer.getNickName());

        if (simpleCustomer.getRole().equals(0)) {
            // 如果是普通用户，可设置为管理员
            adminBtn.setVisibility(View.VISIBLE);
        } else {
            // 如果是管理员，头像更改边框颜色
            headImgView.setBorderColor(this.getColor(R.color.main_border));
        }
    }

    private void initView() {
        titleBar = findViewById(R.id.title_bar);
        titleBar.setLeftClickListener(v -> finish());
        linearLayout5 = findViewById(R.id.linearLayout5);
        headImgView = findViewById(R.id.head_img_view);
        nickNameText = findViewById(R.id.nick_name_text);
        updateNickNameLayout = findViewById(R.id.update_nick_name_layout);
        deleteLayout = findViewById(R.id.delete_layout);

        updateNickNameLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, UpdateFriendNickNameActivity.class);
            intent.putExtra("customerId", simpleCustomer.getCustomerId());
            startActivity(intent);
        });

        deleteLayout.setOnClickListener(v -> {
            if (isBusiness) {
                DeleteBusinessCustomerParam param = new DeleteBusinessCustomerParam();
                param.setCustomerId(simpleCustomer.getCustomerId());
                param.setBusinessId(businessId);
                Type type = new TypeToken<Result<Boolean>>() {
                }.getType();
                OkHttpUtils.getInstance().post(this, RequestEnum.DELETE_BUSINESS_CUSTOMER, type, param, true, (Consumer<Boolean>) b -> {
                    if (b) {
                        runOnUiThread(() -> new MaterialDialog.Builder(this)
                                .iconRes(R.drawable.icon_success)
                                .title(R.string.success)
                                .content("移除成功")
                                .positiveText(R.string.yes).onPositive((dialog, which) -> finish())
                                .show());
                    }
                });
            } else {
                DeleteProjectCustomerParam param = new DeleteProjectCustomerParam();
                param.setCustomerId(simpleCustomer.getCustomerId());
                param.setProjectId(projectId);
                Type type = new TypeToken<Result<Boolean>>() {
                }.getType();
                OkHttpUtils.getInstance().post(this, RequestEnum.DELETE_PROJECT_CUSTOMER, type, param, true, (Consumer<Boolean>) b -> {
                    if (b) {
                        runOnUiThread(() -> new MaterialDialog.Builder(this)
                                .iconRes(R.drawable.icon_success)
                                .title(R.string.success)
                                .content("移除成功")
                                .positiveText(R.string.yes).onPositive((dialog, which) -> finish())
                                .show());
                    }
                });
            }
        });
        adminBtn = findViewById(R.id.admin_btn);
    }
}